﻿using Shop.Collect;
using Shop.ExtendService;
using Shop.LocalEvent;
using Shop.Model.Activity;
using Shop.Model.DB;
using Shop.RemoteModel.Activity.Model;
using Shop.Service.Interface;
using WeDonekRpc.Client;
using WeDonekRpc.Helper;
using WeDonekRpc.Model;

namespace Shop.Service.lmpl
{
    internal class ActivityService : IActivityService
    {
        private readonly IActivityCollect _Activity;
        private readonly IUserGradeCollect _Grade;
        private readonly IEmpService _Emp;
        public ActivityService ( IActivityCollect activity,
            IUserGradeCollect grade,
            IEmpService emp )
        {
            this._Emp = emp;
            this._Activity = activity;
            this._Grade = grade;
        }
        public ActivityData Get ( long id )
        {
            DBShopActivity activity = this._Activity.Get(id);
            ActivityData data = activity.ConvertMap<DBShopActivity, ActivityData>();
            if ( activity.AuditEmpId.HasValue )
            {
                data.AuditEmpName = this._Emp.GetEmpName(activity.AuditEmpId.Value);
            }
            return data;
        }
        public bool OffShelf ( long id )
        {
            DBShopActivity activity = this._Activity.Get(id);
            return this._Activity.OffShelf(activity);
        }
        public bool SetAuditStatus ( long id, AuditResult result )
        {
            DBShopActivity activity = this._Activity.Get(id);
            if ( result.IsPass )
            {
                return this._Activity.AuditPass(activity, result.EmpId);
            }
            else
            {
                return this._Activity.AuditNoPass(activity, result.EmpId, result.Opinion);
            }
        }

        public void Delete ( long id )
        {
            DBShopActivity activity = this._Activity.Get(id);
            this._Activity.Delete(activity);
            new ActivityEvent(activity).AsyncPublic("Delete");
        }
        public bool Set ( long id, ActivitySet data )
        {
            DBShopActivity activity = this._Activity.Get(id);
            return this._Activity.Set(activity, data);
        }
        public long Add ( ActivityAdd add )
        {
            if ( add.ActivityType == RemoteModel.ActivityType.单品秒杀 )
            {
                add.IsLimitBuy = true;
                add.IsLimitBuyNum = true;
                add.MinBuyNum = 1;
                add.MaxBuyNum = 1;
            }
            return this._Activity.Add(add);
        }
        public ActivityBase[] Gets ( long[] ids )
        {
            ActivityDto[] list = this._Activity.Gets<ActivityDto>(ids);
            if ( list.IsNull() )
            {
                return Array.Empty<ActivityBase>();
            }
            return this._Format(list);
        }
        public PagingResult<ActivityBase> Query ( ActivityQueryParam query, IBasicPage paging )
        {
            ActivityDto[] list = this._Activity.Query<ActivityDto>(query, paging, out int count);
            if ( list.IsNull() )
            {
                return new PagingResult<ActivityBase>();
            }
            ActivityBase[] dto = this._Format(list);
            return new PagingResult<ActivityBase>(dto, count);
        }
        private ActivityBase[] _Format ( ActivityDto[] list )
        {
            ActivityBase[] dto = list.ConvertMap<ActivityDto, ActivityBase>();
            Dictionary<long, string> grades = this._Grade.GetName(list.Convert(a => a.UserGrade.HasValue, a => a.UserGrade.Value));
            if ( grades != null )
            {
                dto.ForEach(c =>
                {
                    if ( c.UserGrade.HasValue )
                    {
                        c.GradeName = grades.GetValueOrDefault(c.UserGrade.Value);
                    }
                });
            }
            return dto;
        }
    }
}
